#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;


int main() {
  int N; cin >> N;
  string young, old;
  int youngd, oldd, youngm, oldm, youngy, oldy;
  youngd = youngm = oldd = oldm = 0;
  youngy = 10000;
  oldy = -1;
  int i,j ,k;
  for (i = 0; i < N; ++i) {
    string name; int d, m, y;
    cin >> name >> d >> m >> y;
    if (y < youngy or (youngy == y && m < youngm) or (youngy == y && m == youngm
                                                      && d < youngd))
      young = name, youngd = d, youngm = m, youngy = y;
    if (y > oldy or (oldy == y && m > oldm) or (oldy == y && m == oldm
                                                      && d > oldd))
      old = name, oldd = d, oldm = m, oldy = y;

  } 
  cout << old << endl << young << endl;
  return 0;
}
